DSC 40B Practice Problems

Problems tagged with "recurrence relations"

Problem #011

Tags: recurrence relations

State (but do not solve) the recurrence describing the run time of the following code, assuming that the input, arr, is a list of size n.


def foo(arr, stop=None):
    if stop is None:
        stop = len(arr) - 1

    if stop < 0:
        return 1

    last = arr[stop]
    return last * foo(arr, stop - 1)

Solution

T(n)=T(n1)+Θ(1)

Problem #012

Tags: recurrence relations

What is the solution of the below recurrence? State your answer using asymptotic notation as a function of n.

T(n)={5T(n/5)+Θ(n),n>11,n=1
Solution

Θ(nlogn)

Problem #028

Tags: recurrence relations

State (but do not solve) the recurrence describing the runtime of the following function.


def foo(n):
    if n < 10:
        print("Hello world.")
        return

    for i in range(n):
        print(i)

    for i in range(6):
        foo(n//3)

Solution

T(n)=6T(n/3)+Θ(n).

Problem #044

Tags: recurrence relations

Solve the below recurrence, stating the solution in asymptotic notation. Show your work.

T(n)={2T(n/4)+Θ(n)n>1cn=1
Solution

Θ(n).

We'll unroll several times:

T(n)=2T(n/4)+n=4T(n/16)+n2+n=8T(n/64)+n4+n2+n

The pattern seems to be that on the kth unroll:

T(n)=2kT(n4k)+n(1+12+14++12k1)

The base case is reached when k=log4n. Plugging this back in, we find:

T(n)=2log4nT(1)+n(1+12+14++12log4n1)

If you reached this point, you got most of the credit. If you're unsure of what to do with 2log4n, there are a couple of ways foward.

The first (and easiest) way is to realize that 2log4n is smaller than 2log2n=n, so 2log4n=O(n). Similarly, we've seen the summation of 1+1/2+1/4+ several times -- this is a geometric sum, and converges to 2 when there are infinitely many terms. There are a finite number of terms here, so the sum is <2. Altogether, we have:

T(n)=O(n)T(1)+nΘ(1)=Θ(n)

The second approach is to remember log properties to simplify 2log4n to n. This can be seen by using the ``change of base'' formula:

logbx=logaxlogab.

In this case:

log4n=log2nlog24=log2n2

And therefore 2log4n=2(log2n)/2=(2log2n)1/2=n1/2=n. This shows that the first term in the recurrence is Θ(n). The second term is still Θ(n), so the solution is Θ(n).

Problem #056

Tags: recurrence relations

State (but do not solve) the recurrence describing the runtime of the following function.


def foo(n):
    if n < 1:
        return 0

    for i in range(n**2):
        print("here")

    foo(n/2)

Solution

T(n)={Θ(1),n=1T(n/2)+Θ(n2),n>1

Problem #068

Tags: recurrence relations, mergesort

Consider the modification of mergesort shown below, where one of the recursive calls has been replaced by an in-place version of selection_sort. Recall that selection_sort takes Θ(n2) time.


def kinda_mergesort(arr):
    """Sort array in-place."""
    if len(arr) > 1:
        middle = math.floor(len(arr) / 2)
        left = arr[:middle]
        right = arr[middle:]
        mergesort(left)
        selection_sort(right)
        merge(left, right, arr)

What is the time complexity of kinda_mergesort?

Solution

Θ(n2)

Problem #071

Tags: recurrence relations

Solve the below recurrence, stating the solution in asymptotic notation. Show your work.

T(n)={T(n/2)+Θ(n)n>1Θ(1)n=1
Solution

Θ(n).

Unrolling several times, we find:

T(n)=T(n/2)+n=T(n/4)+n2+n=T(n/8)+n4+n2+n

On the kth unroll, we'll get:

T(n)=T(n/2k)+[n+n2+n4++n2k1]

It will take log2n unrolls to each the base case. Substituting this for k, we get:

T(n)=T(n/2log2n)+[n+n2+n4++n2(log2n)1]=T(1)+n[1+12+14++12(log2n)1]=T(1)+cn=Θ(1)+cn=Θ(n)

Problem #082

Tags: recurrence relations

Solve the recurrence below, stating the solution in asymptotic notation. Show your work.

T(n)={2T(n/4)+Θ(n),n>11,n=1
Solution

Θ(n)

Problem #085

Tags: recurrence relations

State (but do not solve) the recurrence describing the run time of the following code, assuming that the input, arr, is a list of size n.


def foo(arr, stop=None):
    if stop is None:
        stop = len(arr) - 1

    if stop < 0:
        return 1

    last = arr[stop]
    return last * foo(arr, stop - 1)

Solution

T(n)=T(n1)+Θ(1)